package cn.tedu.medicalappointmentsystemk.controller;

import cn.tedu.medicalappointmentsystemk.pojo.bo.AppointmentSelectBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.AppointmentSelectByIdBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.AppointmentSelectByUserIdBO;
import cn.tedu.medicalappointmentsystemk.pojo.param.AppointmentAddParam;
import cn.tedu.medicalappointmentsystemk.pojo.param.AppointmentUpdateParam;
import cn.tedu.medicalappointmentsystemk.pojo.po.PageData;
import cn.tedu.medicalappointmentsystemk.response.JsonResult;
import cn.tedu.medicalappointmentsystemk.service.AppointmentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("v1/appointments")
@Slf4j
@Api(tags = "预约模块")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    @PostMapping("insert")
    @PreAuthorize("hasAuthority('/appointments/insert')")
    @ApiOperationSupport(order = 100)
    @ApiOperation("新增预约")
    public JsonResult addAppointment(AppointmentAddParam param){
        log.info("开始执行新增预约功能,入参:{}",param);
        appointmentService.insert(param);
        log.info("执行新增预约功能结束");
        return JsonResult.ok();
    }

    @GetMapping("{id}/select")
    @PreAuthorize("hasAuthority('/appointments/select')")
    @ApiOperationSupport(order = 110)
    @ApiOperation("预约通过id查询")
    public JsonResult selectAppointmentById(@PathVariable Long id){
        log.info("开始执行通过id:[{}]查询预约用户数据",id);
        AppointmentSelectByIdBO bo = appointmentService.selectAppointmentById(id);
        log.info("执行通过id:[{}]查询预约用户数据结束,出参:{}",id,bo);
        return JsonResult.ok(bo);
    }

    @GetMapping("select/pageNum={pageNum}")
    @PreAuthorize("hasAuthority('/appointments/select')")
    @ApiOperationSupport(order = 120)
    @ApiOperation("预约界面信息查询")
    public JsonResult selectAppointment(@PathVariable Integer pageNum){
        log.info("开始执行预约界面信息查询功能");
        PageData<AppointmentSelectBO> pageData = appointmentService.selectAppointment(pageNum);
        log.info("执行查询预约界面信息结束,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }
    @PostMapping("{id}/delete")
    @PreAuthorize("hasAuthority('/appointments/delete')")
    @ApiOperationSupport(order = 130)
    @ApiOperation("删除单条预约")
    public JsonResult deleteAppointment(@PathVariable Long id){
        log.info("开始执行根据id:[{}]删除单条预约功能",id);
        appointmentService.deleteAppointmentById(id);
        log.info("执行删除单条预约结束");
        return JsonResult.ok();
    }
    @PostMapping("update")
    @PreAuthorize("hasAuthority('/appointments/update')")
    @ApiOperationSupport(order = 140)
    @ApiOperation("修改预约信息")
    public JsonResult updateAppointment(AppointmentUpdateParam param){
        log.info("开始执行修改预约信息功能,入参:{}",param);
        appointmentService.updateAppointment(param);
        log.info("执行修改单条预约结束");
        return JsonResult.ok();
    }
    @GetMapping("selectByUserId/id={id}")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 150)
    @ApiOperation("根据用户Id查询最近一条预约信息")
    public JsonResult selectAppointmentByUserId(@PathVariable Long id){
        log.info("开始执行根据用户id:{}查询最近一条预约信息",id);
        AppointmentSelectByUserIdBO appointmentSelectByUserIdBO = appointmentService.selectAppointmentByUserId(id);
        log.info("执行根据用户id查询最近一条预约信息结束,出参:{}",appointmentSelectByUserIdBO);
        return JsonResult.ok(appointmentSelectByUserIdBO);
    }
    @GetMapping("select/id={id}/pageNum={pageNum}")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 160)
    @ApiOperation("根据用户id查询他的预约信息")
    public JsonResult selectAppointmentsByUserId(@PathVariable Long id,@PathVariable Integer pageNum){
        log.info("开始执行根据用户id:{}查询预约信息",id);
        PageData<AppointmentSelectByUserIdBO> pageData = appointmentService.selectAppointmentsByUserId(id,pageNum);
        log.info("执行根据用户id查询预约信息结束,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/selectTotal")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 170)
    @ApiOperation("查询总预约数")
    public JsonResult selectTotal(){
        return JsonResult.ok(appointmentService.selectTotal());
    }
    @GetMapping("/selectToday")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 180)
    @ApiOperation("查询当日预约数量")
    public JsonResult selectToday(){
        Integer appointments = appointmentService.selectToday();
        return JsonResult.ok(appointments);
    }
    @GetMapping("/searchToday/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 190)
    @ApiOperation("查询当天的预约")
    public JsonResult searchToday(@PathVariable Integer pageNum){
        PageData<AppointmentSelectBO> pageData=appointmentService.searchToday(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/searchLastWeek/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 200)
    @ApiOperation("查询7日内的预约")
    public JsonResult searchLastWeek(@PathVariable Integer pageNum){
        PageData<AppointmentSelectBO> pageData=appointmentService.searchLastWeek(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/searchLastMonth/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 210)
    @ApiOperation("查询30日内的预约")
    public JsonResult searchLastMonth(@PathVariable Integer pageNum){
        PageData<AppointmentSelectBO> pageData=appointmentService.searchLastMonth(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/selectLastMonth")
    @PreAuthorize("hasAnyAuthority('/appointments/select')")
    @ApiOperationSupport(order = 2220)
    @ApiOperation("查询30日内的预约数量")
    public JsonResult selectThisMonth(){
        Integer appointments = appointmentService.selectLastMonth();
        return JsonResult.ok(appointments);
    }

    @GetMapping("selectByNickname/{nickname}")
    @PreAuthorize("hasAuthority('/appointments/select')")
    @ApiOperationSupport(order = 190)
    @ApiOperation("根据患者名字查询医生相关信息(用于搜索)")
    public JsonResult selectByNickname(@PathVariable String nickname){
        log.info("开始执行依据患者姓名:[{}]查询医生信息功能",nickname);
        List<AppointmentSelectBO> appointmentSelectBOS = appointmentService.selectByNickname(nickname);
        log.info("执行依据患者姓名:[{}]查询医生信息功能结束,出参:{}",nickname,appointmentSelectBOS);
        return JsonResult.ok(appointmentSelectBOS);
    }

}
